package com.itheima.springbootmybatis.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.springbootmybatis.domain.User;
import com.itheima.springbootmybatis.mapper.UserMapper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper ;

        @Override
        public Workbook queryUser() {

        List<User> users = userMapper.queryUser();
        String[] title = {"id","姓名","年龄","密码"};
        Workbook wb = new HSSFWorkbook();

        //创建工作表
        Sheet sheet = wb.createSheet();
        Row row = sheet.createRow(0);

        for (int i = 0; i < title.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(title[i]);

        }
        User user = null ;
        for (int i = 0; i < users.size(); i++) {
            sheet.createRow(i+1);
            user = users.get(i);
            row.createCell(0).setCellValue(user.getId());
            row.createCell(1).setCellValue(user.getName());
            row.createCell(2).setCellValue(user.getAge());
            row.createCell(3).setCellValue(user.getPassword());
        }

        return wb ;
    }
}
